package com.sskj.community.ui.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.community.presenter.HotPresenter;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.HotBean;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.lib.widget.TipDialog;

import java.lang.reflect.ParameterizedType;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.COMMUNITY_FRAGMENT_HOT)
public class HotFragment extends BaseFragment<HotPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private int position;
    private BaseAdapter<HotBean.ListBean> adapter;
    private int p = 1;
    private String size = "10";

    @Override
    public int getLayoutId() {
        return R.layout.community_fragment_hot;
    }

    @Override
    public HotPresenter getPresenter() {
        return new HotPresenter();
    }

    @Override
    public void initView() {
        if (SPUtil.get(SPConfig.LOGIN, false)) {
            mPresenter.getHotListLogin(p, size);
        } else {
            mPresenter.getHotList(p, size);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(true)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(Color.TRANSPARENT));
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            p = 1;
            if (SPUtil.get(SPConfig.LOGIN, false)) {
                mPresenter.getHotListLogin(p, size);
            } else {
                mPresenter.getHotList(p, size);
            }
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            p++;
            if (SPUtil.get(SPConfig.LOGIN, false)) {
                mPresenter.getHotListLogin(p, size);
            } else {
                mPresenter.getHotList(p, size);
            }
        });

    }

    @Override
    public void initData() {
        adapter = new BaseAdapter<HotBean.ListBean>(R.layout.community_item_hot, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, HotBean.ListBean item) {
                ImageView iv_zan = holder.getView(R.id.iv_zan);
                LinearLayout itemHotLl = holder.getView(R.id.itemHotLl);
                if (item.getIs_like() == 0) {
                    iv_zan.setEnabled(false);
                } else {
                    iv_zan.setEnabled(true);
                }
                ImageUtil.setCircleImage(BaseHttpConfig.BASE_URL + item.getUser_pic(), holder.getView(R.id.iv_head));
                holder.setText(R.id.tv_name, item.getUsername())
                        .setText(R.id.tv_title, item.getTitle())
                        .setImageResource(R.id.iv_zan, item.getIs_like() == 0 ? R.mipmap.icon_dianzan_no : R.mipmap.icon_dianzan_yes)
                        .setText(R.id.tv_contentItemHot, item.getContent())
                        .setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_share, item.getShare_num())
                        .setText(R.id.tv_zan, item.getLike_num());
                LinearLayout view = holder.getView(R.id.ll_zan);
                view.setOnClickListener(v -> {
                    if (!SPUtil.get(SPConfig.LOGIN, false)) {
                        new TipDialog(getActivity())
                                .setTitleText(App.INSTANCE.getString(R.string.community_hotFragment1))
                                .setContentText(App.INSTANCE.getString(R.string.community_hotFragment2))
                                .setListener(dialog -> ARouter.getInstance().build(RConfig.APP_LOGIN_NUMBER).navigation())
                                .show();
                        return;
                    }
                    //点赞
                    setLike(item.getMsg_id(), item.getIs_like());
                    position = holder.getAdapterPosition();
                });
                TextView share = holder.getView(R.id.tv_share);
                share.setOnClickListener(v -> {
                    ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_SHARE)
                            .withString("msg_id", item.getMsg_id())
                            .navigation();
                });
                itemHotLl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_ARTICLE).withString("like_num", item.getLike_num()).withInt("Is_like", item.getIs_like()).withString("msg_id", item.getMsg_id()).navigation();
                    }
                });
            }
        };
    }

    @Override
    public void onResume() {
        super.onResume();
        if (SPUtil.get(SPConfig.LOGIN, false)) {
            mPresenter.getHotListLogin(p, size);
        } else {
            mPresenter.getHotList(p, size);
        }
    }

    public static HotFragment newInstance() {
        HotFragment fragment = new HotFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @SuppressLint("CheckResult")
    public void getHotListSuccess(HotBean data) {
        if (data == null) {
            return;
        }
        if (data.getList() == null) {
            return;
        }
        if (p == 1) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }

    private void setLike(String item, int is_like) {
        if (is_like == 1) {
            mPresenter.goLike("0", item, false);
        } else {
            mPresenter.goLike("1", item, false);
        }
    }

    int likeNum;

    //点赞或取消点赞成功
    public void setLikeSuccess() {
        adapter.getData().get(position).setIs_like(adapter.getData().get(position).getIs_like() == 1 ? 0 : 1);
        likeNum = Integer.valueOf(adapter.getData().get(position).getLike_num());
        if (likeNum > 0) {
            if (adapter.getData().get(position).getIs_like() == 1) {
                likeNum -= 1;
            } else {
                likeNum += 1;
            }
        }
        adapter.getData().get(position).setLike_num(String.valueOf(likeNum));
        adapter.notifyItemChanged(position);
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
